#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org

START=60
STOP=60

SERVICE_USE_PID=1

NFS_D=/var/lib/nfs
RECOVERY_D=$NFS_D/v4recovery
LOCK_D=/var/lib/nfs/sm
VAR_NFS=/var/lib/nfs

fsid=6669
valid_found=0

allowed_host_star_found="0"
allowed_host_list=""


USE_PROCD=1


update_allowed_host_list()
{
	local host="$1"
	
	if [ "$host" = "*" ] ; then
		allowed_host_star_found="1"
	else
		local host_space=""
		if [ -n "$allowed_host_list" ] ; then host_space=" " ; fi
		allowed_host_list="$allowed_host_list$host_space$host"
	fi
}

add_export_line()
{
	local share="$1"

	vars="name path read_only sync insecure subtree_check anonuid anongid"
	for v in $vars ; do
		config_get "$v" "$share" "$v"
	done
	
	if [ -z "$anonuid" ] ; then
		anonuid=0
	fi
	if [ -z "$anongid" ] ; then
		anongid=0
	fi


	allowed_host_star_found="0"
	allowed_host_list=""
	config_list_foreach "$share" "allowed_hosts" update_allowed_host_list


	
	if [ -n "$name" ] || [ -n "$path" ] ; then
		
		if [ "$read_only" = "1" ] ; then read_only="ro" ; else read_only="rw" ; fi
		if [ "$sync" = "0" ] ; then sync="async" ; else sync="sync" ; fi
		if [ "$secure" = "0" ] ; then secure="insecure" ; else secure="secure" ; fi
		if [ "$subtree_check" = "0" ] ; then subtree_check="no_subtree_check" ; else subtree_check="subtree_check" ; fi
		
		if [ -z "$allowed_host_list" ] || [ "$allowed_host_star_found" = "1" ]; then allowed_host_list="*" ; fi
		allowed_host_list=$( echo "$allowed_host_list" | sed 's/\*/"*"/g' )
		
	
		ln -s "$path" "/nfs/$name"
		for h in $allowed_host_list ; do
			h=$(echo "$h" | sed 's/\"\*\"/*/g')
			echo "/nfs/$name	$h($read_only,$sync,$secure,$subtree_check,root_squash,anongid=$anongid,anonuid=$anonuid,fsid=$fsid)" >> /tmp/tmp_exports
		done
		fsid=$(($fsid+1))
		valid_found=1
	fi
}

start_service() {

	if [ ! -d /tmp/nfs ] ; then
		mkdir /tmp/nfs
	fi
	rm -rf /tmp/nfs/*

	if [ ! -h /nfs ] ; then
		ln -s /tmp/nfs /nfs		
	fi


	#build new exports file from uci
	rm -rf /tmp/tmp_exports
	touch  /tmp/tmp_exports
	config_load "nfsd"
	config_foreach add_export_line "nfsshare"

	if [ "$valid_found" = "0" ] ; then
		stop
		exit;
	fi

	old_md5=$(md5sum /etc/exports 2>/dev/null)
	old_md5=${old_md5% *}
	cur_md5=$(md5sum /tmp/tmp_exports)
	cur_md5=${cur_md5% *}
	if [ "$old_md5" = "$cur_md5" ] && [ -f /etc/exports ] ; then
		rm /tmp/tmp_exports
	else
		mv /tmp/tmp_exports /etc/exports
	fi
	
	mkdir -p $NFS_D
	mkdir -p $RECOVERY_D
	mkdir -p $LOCK_D
	chown nfs:nfs $LOCK_D
	touch $NFS_D/rmtab

	mkdir -p $VAR_NFS
	chown nfs:nfs $VAR_NFS

	sysctl -w fs.nfs.nlm_tcpport=32777 fs.nfs.nlm_udpport=32777 > /dev/null


	procd_open_instance
	procd_set_param command /usr/sbin/rpc.statd -p 32778 -o 32779 -F
	procd_close_instance

	/usr/sbin/exportfs -r
	/usr/sbin/rpc.nfsd --grace-time 10

	procd_open_instance
	procd_set_param command /usr/sbin/rpc.mountd -p 32780 -F
	procd_close_instance
}

stop_service() {

	service_stop /usr/sbin/rpc.mountd
	rpc.nfsd 0 2>&1 /dev/null
	/usr/sbin/exportfs -au 2>&1 >/dev/null
	grep -q /proc/fs/nfsd /proc/mounts 2>/dev/null && umount /proc/fs/nfsd >/dev/null 2>&1
	service_stop /usr/sbin/rpc.statd >/dev/null 2>&1

}

reload() {
	/usr/sbin/exportfs -r
}

